<?php

class Backend_Model_DbTable_Booking extends Zend_Db_Table_Abstract
{

    protected $_name = 'booking';

    public function getAllBookings($cond = null, $joinCancel = false){
        //Construct where clause
        $where = "";
        foreach($cond as $val){
            if(strlen($where)>0) $where .= " AND ";

            $where .= "$val";
        }
        
        $query = $this->select();
        $query->from(array('b'=>'booking'));
        $query->joinInner(array('bs'=>'booking_status'), 'b.booking_status_id = bs.booking_status_id', array('bstatus_name'=>'bs.name'));
        $query->joinInner(array('ps'=>'payment_status'), 'b.payment_status_id = ps.payment_status_id', array('pstatus_name'=>'ps.name'));
        $query->joinInner(array('v'=>'venue'), 'b.venue_id = v.venue_id', array('v_name'=>'v.name'));
        $query->joinInner(array('u'=>'user'), 'b.user_id = u.user_id', array('u_name'=>'u.full_name','sales_id'=>'u.sales_id'));
        $query->order('b.booking_time');
        $query->setIntegrityCheck(false);

        if($joinCancel) $query->joinLeft(array('bc'=>'booking_cancel'), 'b.booking_id = bc.booking_id', array('reason'=>'bc.reason'));
        if(!is_null($cond)) $query->where($where);
        
        return $this->fetchAll($query);
    }

    public function getBooking($key,$value){
        if(is_string($value)) $value = "'$value'";

        $query = $this->select();
        $query->from(array('b'=>'booking'));
        $query->joinInner(array('bs'=>'booking_status'), 'b.booking_status_id = bs.booking_status_id', array('bstatus_name'=>'bs.name'));
        $query->joinInner(array('ps'=>'payment_status'), 'b.payment_status_id = ps.payment_status_id', array('pstatus_name'=>'ps.name'));
        $query->joinInner(array('v'=>'venue'), 'b.venue_id = v.venue_id', array('v_name'=>'v.name'));
        $query->joinInner(array('u'=>'user'), 'b.user_id = u.user_id', array('u_name'=>'u.full_name'));
        $query->joinLeft(array('bc'=>'booking_cancel'), 'b.booking_id = bc.booking_id', array('reason'=>'bc.reason'));
        $query->where("b.$key = $value");
        $query->setIntegrityCheck(false);

        return $this->fetchRow($query);
    }
    
}

